﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utilities;
using System.Data;
using Domain.DAL;
using Domain.Models;
using Telerik.Web.UI;

namespace WebApp.Manufacturers
{
    public partial class ManufacturerManagement : System.Web.UI.Page
    {
        #region RadGrid's Event Handlers

        protected void grdResult_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                Utility.TranslateGridPagerItem(e);
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void grdResult_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            try
            {
                BindGrid();
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void grdResult_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                string id = MyConfig.NullNumber.ToString();
                switch (e.CommandName)
                {
                    case "cmdEdit":
                        id = ((GridEditableItem)e.Item)["ID"].Text;
                        string url = this.ResolveUrl("~/Manufacturers/ManufacturerEdit.aspx") + "?ID=" + id;
                        Response.Redirect(url);
                        break;
                    case "cmdDelete":
                        id = ((GridEditableItem)e.Item)["ID"].Text;
                        if (DeleteManufacturer(id))
                        {
                            Utility.ShowMessageBox(this.Page, Resources.Strings.DeleteSuccess);
                        }
                        else
                        {
                            Utility.ShowMessageBox(this.Page, Resources.Strings.DeleteFail);
                        }

                        grdResult.Rebind();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        #endregion

        #region Event Handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                (Master as SiteMaster).SelectedTab = "systemAdministrator";
                if (!IsPostBack)
                {
                    pageTitle.Text = Resources.Strings.SystemAdministrator + @"\" + Resources.Strings.ManufacturerManagement;
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.ResolveUrl("~/Manufacturers/ManufacturerEdit.aspx"));
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            grdResult.Rebind();
        }

        #endregion

        #region Private Methods

        private void BindGrid()
        {
            try
            {
                DataSet ds = new DataSet();
                ManufacturerDAO mDAO = ManufacturerDAO.GetInstance();
                Manufacturer m = GetManufacturerInfo();

                if (mDAO != null && m != null)
                {
                    ds = mDAO.Search(m);
                    grdResult.DataSource = ds;
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        private Manufacturer GetManufacturerInfo()
        {
            Manufacturer mManufacturer = new Manufacturer();
            try
            {
                mManufacturer.Name = txtName.Text.Trim();
            }
            catch (Exception)
            {
                mManufacturer = null;
                throw;
            }
            return mManufacturer;
        }

        private bool DeleteManufacturer(string id)
        {
            bool result = false;
            try
            {
                ManufacturerDAO mDAO = ManufacturerDAO.GetInstance();
                if (mDAO != null)
                {
                    result = mDAO.Delete(id);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
        #endregion
    }
}